package com.serecttask.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.serecttask.R;
import com.serecttask.model.JsonModel;

import java.util.HashMap;

import util.KsoapUtil;
import util.PostCallback;
import util.StringUtil;
import util.ToastUtils;
import widget.Boolean_Mobile;
import widget.ClearEditText;
import widget.Timer;

/**
 * Created by Administrator on 2016/12/15.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private ClearEditText et_register_username = null;
    private ClearEditText et_register_phone_num = null;
    private ClearEditText et_register_password = null;
    private ClearEditText et_register_code = null;

    private Button btn_register_button;
    private Button btn_register_sendVerificationCode;
    private String userName;
    private String passWord;
    private String phoneNumber;
    private String phoneNumberLatest;
    private String verificationCode;
    private String verificationCodeReceive;
    private Timer time;
    private TextView tv_register_protocol;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initWidget();
    }
    private void initView() {
        initTopBar("注册");
        et_register_username = (ClearEditText) findViewById(R.id.et_register_username);
        et_register_phone_num = (ClearEditText) findViewById(R.id.et_register_phone_num);
        et_register_password = (ClearEditText) findViewById(R.id.et_register_password);
        et_register_code = (ClearEditText) findViewById(R.id.et_register_code);

        btn_register_button = (Button) findViewById(R.id.btn_register_button);
        btn_register_sendVerificationCode = (Button) findViewById(R.id.btn_register_sendVerificationCode);
        tv_register_protocol = (TextView) findViewById(R.id.tv_register_protocol);
        tv_register_protocol.setOnClickListener(this);
        time = new Timer(60000, 1000,btn_register_sendVerificationCode);
    }

    private void initWidget() {
        btn_register_button.setOnClickListener(this);
        btn_register_sendVerificationCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_top_Back:
                finish();
                break;
            case R.id.btn_register_sendVerificationCode:
                //获取本机号码并下发验证码！
                phoneNumber = et_register_phone_num.getText().toString();
                if( Boolean_Mobile.isMobileNO(phoneNumber)){
                    time.start();// 开始计时
                    sendVerificationCode();
                } else {
                    ToastUtils.show(RegisterActivity.this, "请重新输入手机号", 1);
                }
                break;
            case R.id.btn_register_button:
                register();
                break;
            case R.id.tv_register_protocol:
             /*   startActivityForResult(new Intent(RegisterActivity.this, WebLoadActivity.class), 0);*/
                break;
            default:
                break;

        }
    }

    /*发送验证码*/
    private void sendVerificationCode() {
        HashMap<String, String> params = new HashMap<>();
        params.put("Mobile", phoneNumber);
        params.put("Type", "UserRegister");
        KsoapUtil ksoapUtil = new KsoapUtil("SendVerCode", params, new PostCallback() {
            @Override
            public void onSuccess(String result) {
                JsonModel jsonModel = JSON.parseObject(result, JsonModel.class);
                Log.i("JsonModel","JsonModel" + jsonModel);
                if (jsonModel.isSuccess()) {
                    verificationCodeReceive = jsonModel.getMessage();
                }
            }
            @Override
            public void onFail(Exception e) {
                ToastUtils.show(RegisterActivity.this, "网络异常", 1);
                e.printStackTrace();
            }
        });
        ksoapUtil.execute();
    }

    /*注册*/
    private void register() {
        userName = et_register_username.getText().toString();
        phoneNumberLatest = et_register_phone_num.getText().toString();
        passWord = et_register_password.getText().toString();
        verificationCode = et_register_code.getText().toString();

        if (StringUtil.isNullOrEmpty(userName)) {
            ToastUtils.show(RegisterActivity.this,"用户名不能为空",1);
        } else if (userName.length() > 20) {
            ToastUtils.show(RegisterActivity.this,"用户名最多不能超过20个字符",1);
        } else if (StringUtil.isNullOrEmpty(phoneNumberLatest)) {
            ToastUtils.show(RegisterActivity.this,"手机号不能为空",1);
        } else if (StringUtil.isNullOrEmpty(verificationCode)){
            ToastUtils.show(RegisterActivity.this,"验证码不能为空",1);
        } else if (!Boolean_Mobile.isMobileNO(phoneNumberLatest)) {
            ToastUtils.show(RegisterActivity.this,"请输入正确的手机号",1);
        } else if (!verificationCode.equals(verificationCodeReceive) || !phoneNumber.equals(phoneNumberLatest)){
            ToastUtils.show(RegisterActivity.this,"验证码不正确",1);
        } else if (StringUtil.isNullOrEmpty(passWord)){
            ToastUtils.show(RegisterActivity.this,"密码不能为空",1);
        } else if (!Boolean_Mobile.isPassWord(passWord)) {
            ToastUtils.show(RegisterActivity.this,"密码长度应为6~15个字符，请重新输入",1);
        } else {
            HashMap<String, String> params = new HashMap<>();
            params.put("Mobile",phoneNumber);
            params.put("Pwd",passWord);
            params.put("UserName", userName);

            KsoapUtil ksoapUtil = new KsoapUtil("Register", params, new PostCallback() {
                @Override
                public void onSuccess(String result) {
                    JsonModel jsonModel = JSON.parseObject(result, JsonModel.class);
                    Log.i("JsonModel", "jsonModel" + jsonModel);
                    if (jsonModel.isSuccess()) {
                        ToastUtils.show(RegisterActivity.this,"注册成功！", 1);
                        finish();
                    } else {
                        ToastUtils.show(RegisterActivity.this,jsonModel.getMessage(), 1);
                    }
                }

                @Override
                public void onFail(Exception e) {
                    ToastUtils.show(RegisterActivity.this, "访问异常", 1);
                    e.printStackTrace();
                }
            });
            ksoapUtil.execute();
        }
    }
}
